package com.joolun.web.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.mall.entity.*;
import com.joolun.mall.service.*;
import com.joolun.weixin.utils.ThirdSessionHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/api/ma/collect")
@Api(value = "collect", tags = "收藏")
public class CollectApi {
    private final CollectService baseService;
    private final WorkService workService;
    private final ActiveService activeService;
    private  final VideoService videoService;
    @ApiOperation(value = "查询实习")
    @PostMapping("/save")
    public AjaxResult getWishCate(@RequestBody Collect collect) {
        List<Collect> list = baseService.list(new QueryWrapper<Collect>().eq("user_id", collect.getUserId()).eq("bizId", collect.getBizId()).eq("type",collect.getType()));
        if(list.size() == 0){
            baseService.save(collect);
            return AjaxResult.success();
        }else{
            return AjaxResult.error("已经收藏过");
        }

    }

    @ApiOperation(value = "通过type")
    @GetMapping("/list/getByType")
    public AjaxResult getCollectByType( Collect collect) {
        Integer type = collect.getType();
        List<Collect> list = baseService.list(
                new QueryWrapper<Collect>().eq("type", collect.getType()).eq("user_id",collect.getUserId()));
        for (int i = 0; i < list.size(); i++) {
            Collect c = list.get(i);
            if(type==1){
                Work work = workService.getById(c.getBizId());
                if(work != null){
                    c.setWork(work);
                }
            }else if(type == 2){
                Active active = activeService.getById(c.getBizId());
                if(active != null) {
                    c.setActive(active);
                }
            }else if(type == 3){
                Video video = videoService.getById(c.getBizId());
                if(video != null) {
                    c.setVideo(video);
                }
            }
        }
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "通过id删除分类")
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable String id){
        return AjaxResult.success(baseService.removeById(id));
    }

}
